package com.media.revision.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.media.AlbumManageUtils;
import com.media.MediaGlideLoader;
import com.media.R;
import com.media.image.ImageModel;
import com.media.image.ImageWork;

import java.util.ArrayList;
import java.util.Formatter;

public class RevisionAdapter extends BaseAdapter {
    public int selectType = 0;
    public Activity mActivity;
    public ImageWork mImageWork;
    private ArrayList<ImageModel> itemImageList = new ArrayList<>();//所有的相册和视频图片
    private ArrayList<ImageModel> selecImageList = new ArrayList<>();//选中图片
    private StringBuilder mFormatBuilder;
    private Formatter mFormatter;
    private String mediaType;

    public void setSelectImageList(ArrayList<ImageModel> selecImageList) {
        this.selecImageList = selecImageList;
    }

    public void setItemImageList(ArrayList<ImageModel> itemImageList) {
        this.itemImageList = itemImageList;
    }

    public void setmFormatBuilder(StringBuilder mFormatBuilder) {
        this.mFormatBuilder = mFormatBuilder;
    }

    public void setmFormatter(Formatter mFormatter) {
        this.mFormatter = mFormatter;
    }

    public RevisionAdapter(Activity mActivity, ImageWork mImageWork, String mediaType) {
        this.mActivity = mActivity;
        this.mImageWork = mImageWork;
        this.mediaType = mediaType;
    }

    public void setSelectType(int selectType) {
        this.selectType = selectType;
    }

    @Override
    public int getCount() {
        return itemImageList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemImageList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        ImageModel imageModel = (ImageModel) getItem(i);
        String path = imageModel.getPath();
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mActivity.getApplication()).inflate(R.layout.revision_media_view_media_imageview, null);
            viewHolder.relativeView = view.findViewById(R.id.rl_view);
            viewHolder.imageView = view.findViewById(R.id.iv_imageView);
            viewHolder.iv_select = view.findViewById(R.id.iv_select);
            viewHolder.mIvNoSelect = view.findViewById(R.id.iv_no_select);
            viewHolder.ll_select = view.findViewById(R.id.ll_select);
            viewHolder.bottom = view.findViewById(R.id.bottom);
            viewHolder.videoTime = view.findViewById(R.id.videoTime);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }

        if (selectType == 1) {//图片被选中
            AlbumManageUtils.selectImg(imageModel, viewHolder.relativeView, viewHolder.imageView, viewHolder.ll_select);
        } else if (selectType == 2) {//视频被选中
            AlbumManageUtils. selectVideo(imageModel, viewHolder.relativeView, viewHolder.imageView, viewHolder.ll_select);
        } else {
//            viewHolder.relativeView.setAlpha(1f);
            viewHolder.imageView.setAlpha(1f);
            viewHolder.ll_select.setAlpha(1f);
            viewHolder.imageView.setEnabled(true);
            viewHolder.ll_select.setEnabled(true);
            viewHolder.relativeView.setEnabled(true);
        }
        if (selecImageList.contains(imageModel) && imageModel.getIsChecked()) {
            viewHolder.iv_select.setVisibility(View.VISIBLE);
            viewHolder.iv_select.setText(String.valueOf(imageModel.getSelsctRanking()));
            viewHolder.mIvNoSelect.setVisibility(View.GONE);
        } else {
            viewHolder.iv_select.setVisibility(View.GONE);
            viewHolder.mIvNoSelect.setVisibility(View.VISIBLE);
        }
        if (imageModel.getType().equals(ImageModel.TYPE_IMAGE)
                || imageModel.getType().equals(ImageModel.TYPE_CAMERA)) {
            viewHolder.bottom.setVisibility(View.GONE);
        } else if (imageModel.getType().equals(ImageModel.TYPE_VIDEO)) {
            viewHolder.bottom.setVisibility(View.VISIBLE);
            viewHolder.videoTime.setText(stringForTime(imageModel.duration));
        }

        if (imageModel.type.equals(ImageModel.TYPE_CAMERA)) {
            MediaGlideLoader.LoderDrawable(mActivity, R.mipmap.media_ic_camera, viewHolder.imageView, 0);
        } else {
            mImageWork.loadImage(imageModel.getType(), path, viewHolder.imageView);
        }
        viewHolder.initListener(i, imageModel);
        return view;
    }

    class ViewHolder {
        RelativeLayout relativeView;
        ImageView imageView;
        ImageView mIvNoSelect;
        TextView iv_select;
        LinearLayout ll_select;
        View bottom;
        TextView videoTime;

        public void initListener(final int i, final ImageModel imageModel) {
            ll_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnSelectClickListener != null)
                        mOnSelectClickListener.onSelectClick(i, imageModel,mediaType);
                }
            });
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mOnPreviewClickListener != null)
                        mOnPreviewClickListener.onPreviewClick(i, imageModel);
                }
            });
        }
    }

    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;
        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%01d:%02d", minutes, seconds).toString();
        }
    }

    private OnSelectClickListener mOnSelectClickListener;
    private OnPreviewClickListener mOnPreviewClickListener;

    public void setOnSelectClickListener(OnSelectClickListener onSelectClickListener) {
        this.mOnSelectClickListener = onSelectClickListener;
    }

    public void setOnPreviewClickListener(OnPreviewClickListener onPreviewClickListener) {
        this.mOnPreviewClickListener = onPreviewClickListener;
    }

    public interface OnSelectClickListener {
        void onSelectClick(int i, ImageModel imageModel, String type);
    }

    public interface OnPreviewClickListener {
        void onPreviewClick(int i, ImageModel imageModel);
    }

}